var MainVideoCheckedItems;
function getMainVideoData() {
	var startDate = $("#MainVideoStartDate").val();
	var endDate = $("#MainVideoEndDate").val();
	var osID = document.getElementById("MainVideoOS").options[document.getElementById("MainVideoOS").selectedIndex].value;
	var typeID = document.getElementById("MainVideoType").options[document.getElementById("MainVideoType").selectedIndex].value;
	var marketID = document.getElementById("MainVideoMarket").options[document.getElementById("MainVideoMarket").selectedIndex].value;
	$(".MainVideoChart").html("");
	if (startDate != "" && endDate != "")
	{
		//统计项
		var items = document.getElementsByName("MainVideoCheckbox1");
		MainVideoCheckedItems = new Array();
		for(var i=0;i<items.length;i++) {
			if (items[i].checked)
			{
				MainVideoCheckedItems.push(items[i].value);
			}
		}
		items = document.getElementsByName("MainVideoCheckbox2");
		for(var i=0;i<items.length;i++) {
			if (items[i].checked)
			{
				MainVideoCheckedItems.push(items[i].value);
			}
		}


		if(MainVideoCheckedItems.length > 0) {
			var start = new Date();
			var end = new Date();

			jQuery('#body-wrapper').showLoading();
			start.setFullYear(startDate.substring(0,4), startDate.substring(5,7)-1, startDate.substring(8,10));
			end.setFullYear(endDate.substring(0,4), endDate.substring(5,7)-1 ,endDate.substring(8,10));

			days = parseInt((end - start)/1000/60/60/24)+1;

			var APIurl = "/client/mainVideoAnalytics?staDate="+startDate+"&pd="+days;
			if (osID != "")
				APIurl += "&os="+osID;
			if (typeID != "")
				APIurl += "&dt="+typeID;
			if (marketID != "")
				APIurl += "&pm="+marketID;

			
			$.ajax({
				type: "GET",
				url: APIurl,
				dataType : "jsonp",
				jsonnp: "callback",
				success: loadMainVideoData
			});
		}
		else
			alert("请选择至少一个统计项");
	}
	else
		alert("请选择时间区域");
}


var MainVideoTabArr, MainVideoDetailArr, MainVideoSumArr, MainVideoDateArr, MainSubTitle;
function loadMainVideoData(data) {
	var osID = document.getElementById("MainVideoOS").options[document.getElementById("MainVideoOS").selectedIndex].value;
	var typeID = document.getElementById("MainVideoType").options[document.getElementById("MainVideoType").selectedIndex].value;
	var marketID = document.getElementById("MainVideoMarket").options[document.getElementById("MainVideoMarket").selectedIndex].value;
	var tab = "";
	var startDate = $("#MainVideoStartDate").val();
	var endDate = $("#MainVideoEndDate").val();
	MainSubTitle = startDate.replace(/\//g, "-") + " 至 " + endDate.replace(/\//g, "-");
	jQuery('#body-wrapper').hideLoading();	


	MainVideoTabArr = new Array();
	MainVideoDetailArr = new Array();
	MainVideoSumArr = new Array();
	MainVideoDateArr = new Array();

	//读取数据
	if (data.items.length > 0) //平台选择全部
	{
		changeChartType('MainVideo', 'Line');
		if (osID == "") //平台选择全部
		{	
			//显示选择Tab
			genTabs("MainVideo", "MainVideoOS", MainVideoTabArr, MainVideoDetailArr, MainVideoSumArr, MainVideoCheckedItems);
			//读取数据
			readData(data, MainVideoDateArr, "os", MainVideoTabArr, MainVideoDetailArr, MainVideoCheckedItems, MainVideoSumArr);
			$("#MainVideoPieTab").show();
			$("#MainVideoTab").show();
            

			//生成图表
			genSummaryTable("MainVideoTable", valueFullNameArr(MainVideoCheckedItems), MainVideoDateArr, MainVideoSumArr, -1, "");
			//生成饼图
			genSelPieChart(MainVideoCheckedItems, "MainVideoPie", MainVideoTabArr, MainVideoDetailArr, MainSubTitle);
			//生成线形图
			genSummaryLineChart("MainVideoLine", "总览趋势图", MainVideoDateArr, valueFullNameArr(MainVideoCheckedItems), MainVideoSumArr, MainSubTitle);

		}
		else if (typeID == "") //终端选择全部
		{
			//显示选择Tab
			genTabs("MainVideo", "MainVideoType", MainVideoTabArr, MainVideoDetailArr, MainVideoSumArr, MainVideoCheckedItems);
			//读取数据
			readData(data, MainVideoDateArr, "type", MainVideoTabArr, MainVideoDetailArr, MainVideoCheckedItems, MainVideoSumArr);
			//生成图表
			genSummaryTable("MainVideoTable", valueFullNameArr(MainVideoCheckedItems), MainVideoDateArr, MainVideoSumArr, -1, "");
			//生成饼图
			$("#MainVideoPieTab").show();
			$("#MainVideoTab").show();
			genSelPieChart(MainVideoCheckedItems, "MainVideoPie", MainVideoTabArr, MainVideoDetailArr, MainSubTitle);
			//生成线形图
			genSummaryLineChart("MainVideoLine", "总览趋势图", MainVideoDateArr, valueFullNameArr(MainVideoCheckedItems), MainVideoSumArr, MainSubTitle);

		}
		else if (marketID == "") //市场选择全部
		{
			if (osID != "android")
			{
				for(n=0; n<MainVideoCheckedItems.length; n++) 
					MainVideoSumArr[n] = new Array();
				$("#MainVideoPieTab").hide();
				$("#MainVideoPie").html("");
				$("#MainVideoTab").hide();
			}
			else {
				genTabs("MainVideo", "MainVideoMarket", MainVideoTabArr, MainVideoDetailArr, MainVideoSumArr, MainVideoCheckedItems);
				$("#MainVideoTab").show();
				$("#MainVideoPieTab").show();
			}
			//读取数据
			readData(data, MainVideoDateArr, "pm", MainVideoTabArr, MainVideoDetailArr, MainVideoCheckedItems, MainVideoSumArr);
			//生成图表
			genSummaryTable("MainVideoTable", valueFullNameArr(MainVideoCheckedItems), MainVideoDateArr, MainVideoSumArr, -1, "");
			//生成线形图
			genSummaryLineChart("MainVideoLine", "总览趋势图", MainVideoDateArr, valueFullNameArr(MainVideoCheckedItems), MainVideoSumArr, MainSubTitle);
			//显示选择Tab
			if (osID == "android") {
				//生成饼图
				genSelPieChart(MainVideoCheckedItems, "MainVideoPie", MainVideoTabArr, MainVideoDetailArr, MainSubTitle);
			}
		}
		else //平台、终端、市场均不为全部
		{
			for(n=0; n<MainVideoCheckedItems.length; n++) 
				MainVideoSumArr[n] = new Array();
			$("#MainVideoPieTab").hide();
			$("#MainVideoPie").html("");
			$("#MainVideoTab").hide();

			//读取数据
			readData(data, MainVideoDateArr, "", MainVideoTabArr, MainVideoDetailArr, MainVideoCheckedItems, MainVideoSumArr);
			//生成图表
			genSummaryTable("MainVideoTable", valueFullNameArr(MainVideoCheckedItems), MainVideoDateArr, MainVideoSumArr, -1, "");
			//生成线形图
			genSummaryLineChart("MainVideoLine", "总览趋势图", MainVideoDateArr, valueFullNameArr(MainVideoCheckedItems), MainVideoSumArr, MainSubTitle);
		}
	}
	else {
		alert("选择的时间区间内无数据");	
	}
}

function changeMainVideoTab(tabName, optionName) {
	if (tabName == "All")
	{
		$("#MainVideoPieTab").show();
		//生成饼图
		genSelPieChart(MainVideoCheckedItems, "MainVideoPie", MainVideoTabArr, MainVideoDetailArr, MainSubTitle);
		//生成线形图
		genSummaryLineChart("MainVideoLine", "总览趋势图", MainVideoDateArr, valueFullNameArr(MainVideoCheckedItems), MainVideoSumArr, MainSubTitle);
		//生成表格
		genSummaryTable("MainVideoTable", valueFullNameArr(MainVideoCheckedItems), MainVideoDateArr, MainVideoSumArr, -1, "");
	}
	else {
		$("#MainVideoPieTab").hide();
		$("#MainVideoPie").html("");
		//生成线形图
		genSummaryLineChart("MainVideoLine", "总览趋势图", MainVideoDateArr, valueFullNameArr(MainVideoCheckedItems), MainVideoDetailArr[tabName], MainSubTitle);
		//生成表格
		genSummaryTable("MainVideoTable", valueFullNameArr(MainVideoCheckedItems), MainVideoDateArr, MainVideoDetailArr, tabName, MainVideoTabArr);

		if ($("#MainVideoPieTab").hasClass("selbitem"))
			changeChartType('MainVideo', 'Line');
			
	} 
	$(".bitem").removeClass("selbitem");
	$("#MainVideoTab"+tabName).addClass("selbitem");
	
}


